package com.pesilo.mvc.view.component.module.backgroundModule
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.pesilo.base.BaseComponent;
	import com.pesilo.event.PesiloEvent;
	import com.pesilo.mvc.model.vo.BackgroundImageVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;

	public class bgComponent extends BaseComponent
	{
		private var vo:BackgroundImageVO;
		
		private var bgBit:Bitmap;
		private var alterBit:Bitmap;
		private var bulkLoader:BulkLoader;
		private var bmdArr:Array = [];
		private var curIndex:int = -1;
		private var scaleNum:Number = 0.2;
		
		private var loader:Loader;
		public function bgComponent(vo:BackgroundImageVO) 
		{
			this.vo=vo;
			super();
		}
		
		override public function initUI():void {
//			if (settingproxy.bgBmp)bmdArr.push(settingproxy.bgBmp);
			loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,__loaderCompleteHandle);
			loader.load(new URLRequest(vo.list[0].img));
		}
		
		protected function __loaderCompleteHandle(e:Event):void
		{
			var bmp:Bitmap=loader.content as Bitmap;
			bmdArr.push(bmp.bitmapData);
			
			if (vo.list.length>1) {
				bulkLoader = new BulkLoader("bg");
				for (var i:uint = 1; i < vo.list.length; i++ ) {
					bulkLoader.add(vo.list[i].img, { "id":'bg_' + i } );
					
				}
				bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, __loadCompleteHandle, false, 0, true);
				bulkLoader.start();
			}	
			if(bmdArr.length)
				this.addChild(createNextBitmap());
		}
		
		private function __moveEndHandle(e:PesiloEvent):void 
		{
			(e.currentTarget as BgItemComponent).removeEventListener(PesiloEvent.EVENT_BACKGROUD_MOVE_END, __moveEndHandle);
			var bgItemComponent:BgItemComponent = new BgItemComponent(createNextBitmap());
			this.addChild(bgItemComponent)
			bgItemComponent.addEventListener(PesiloEvent.EVENT_BACKGROUD_MOVE_END,__moveEndHandle,false,0,true);
		}
		
		private function __loadCompleteHandle(e:BulkProgressEvent):void 
		{
			this.removeEventListener(BulkProgressEvent.COMPLETE, __loadCompleteHandle);
			if (vo.list.length > 1) {
				for (var i:uint = 1; i < vo.list.length; i++ ) {
					bmdArr.push(bulkLoader.getBitmap("bg_"+i).bitmapData);
				}
			}
//			while (this.numChildren) this.removeChildAt(0);
			var bgItemComponent:BgItemComponent = new BgItemComponent(this.getChildAt(0) as Bitmap,true);
			this.removeChildAt(0);
			this.addChild(bgItemComponent)
			
			
			bgItemComponent.addEventListener(PesiloEvent.EVENT_BACKGROUD_MOVE_END,__moveEndHandle,false,0,true);
		}
		
		public function createNextBitmap():Bitmap {			
			curIndex += 1;
			if (curIndex > (bmdArr.length - 1)) curIndex = 0;
			var bitmap:Bitmap = new Bitmap(bmdArr[curIndex]) as Bitmap;
			setBitSize(bitmap);
			bitmap.x = getRandom() * stage.stageWidth * scaleNum;
			bitmap.y = getRandom() * stage.stageHeight * scaleNum;
			
			return bitmap;
		}
		
		public function setBitSize(bitmap:Bitmap):void {
			var w:Number = stage.stageWidth*(1+scaleNum);
			var h:Number = stage.stageHeight * (1 + scaleNum);
			if ((bitmap.width / bitmap.height) < (w / h)) {
				bitmap.height = bitmap.height * w / bitmap.width;
				bitmap.width =w;
			}else {
				bitmap.width = bitmap.width * h / bitmap.height;
				bitmap.height = h;
			}
		}
		
		public function getRandom():int {
			if (Math.random() > 0.5) return -1;
			else return 0;
		}
		
		override public function initLocation():void {
			for (var i:int = numChildren - 1; i > -1; i-- ) {
				if (this.getChildAt(i) is BgItemComponent)	{
					(this.getChildAt(i) as BgItemComponent).stop();
				}else {
					setBitSize(this.getChildAt(i) as Bitmap);
				}
			}
		}
		
		override public function initEvent(b:Boolean = true):void {
			
		}
	}

}